DataWeave Playground License Agreement
By accessing and using this DataWeave Playground, you agree to the DataWeave Playground Commercial Free License Agreement located here.
You also agree that you will not submit any personal data or personally identifiable information, personal health data, or other sensitive or regulated data into the DataWeave Playground and that MuleSoft may store, monitor, track, or inspect data that is accessible by the DataWeave Playground.
Expression Type: Array<"a" | "b" | "c" | "d"> {"mediaType": "application/json", "encoding": "UTF-8"}
(items: Array<T> | Array<T> | String | Null, callback: (item: T, accumulator: T) -> T | (item: T, accumulator: A) -> A | (item: String, accumulator: String) -> String | (item: String, accumulator: A) -> A | (item: T, accumulator: A) -> A) -> T | A | String | A | Null
Applies a reduction expression to the elements in an array.
For each element of the input array, in order, reduce applies the reduction
lambda expression (function), then replaces the accumulator with the new
result. The lambda expression can use both the current input array element
and the current accumulator value.
Note that if the array is empty and no default value is set on the accumulator parameter, a null value is returned.
[%header, cols="1,3"]
|===
| Name | Description
| item | Item in the input array. It provides the value to reduce. Can also be referenced as $.
| acc | The accumulator. Can also be referenced as $$. Used to store the result of the lambda expression after each iteration of the reduce operation.
The accumulator parameter can be set to an initial value using the
syntax acc = initValue. In this case, the lambda expression is
called with the first element of the input array. Then the result
is set as the new accumulator value.
If an initial value for the accumulator is not set, the accumulator is set to the first element of the input array. Then the lambda expression is called with the second element of the input array.
The initial value of the accumulator and the lambda expression
dictate the type of result produced by the reduce function. If
the accumulator is set to acc = {}, the result is usually of type
Object. If the accumulator is set to acc = [], the result
is usually of type Array. If the accumulator is set to acc = "",
the result is usually a String.
|===
This example returns the sum of the numeric values in the first input array.
[source,DataWeave,linenums]
[source,JSON,linenums]
This example adds the numbers in the sum example, concatenates the same
numbers in concat, and shows that an empty array [] (defined in
myEmptyList) returns null in emptyList.
[source,DataWeave,linenums]
[source,JSON,linenums]
This example sets the first element from the first input array to "z", and
it adds 3 to the sum of the second input array. In multiply, it shows how
to multiply each value in an array by the next
([2,3,3] reduce ((item, acc) -> acc * item)) to
produce a final result of 18 (= 2 * 3 * 3). The final example,
multiplyAcc, sets the accumulator to 3 to multiply the result of
acc * item (= 12) by 3 (that is, 3 (2 * 2 * 3) = 36), as shown in
the output.
[source,DataWeave,linenums]
[source,JSON,linenums]
This example shows a variety of uses of reduce, including its application to
arrays of boolean values and objects.
[source,DataWeave,linenums]
[source,JSON,linenums]